package org.q;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.commons.lang.StringUtils;
import org.q.threads.MainThread;

public class StartMain {
	
	public static volatile boolean SHUTDOWN = false;
	
	public static void main(String[] args) throws InterruptedException {
		new StopListener().start();
		new MainThread().proc();
	}

}
class StopListener extends Thread{
	
	private ServerSocket server;
	private Socket socket;
	private BufferedReader in;
	
	public void run() {
	
		try {
			server = new ServerSocket(10987);
			while (true) {
				socket = server.accept();
				in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
				String line = in.readLine();
				if(StringUtils.isNotBlank(line)) {
					StartMain.SHUTDOWN = true;
				}
				in.close();
				socket.close();
			}
		} catch (IOException e) {
			try {
				server.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
}
